package com.foreveross.bsl.mam.device.application.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.inject.Named;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.mam.device.application.DeviceUserApplication;
import com.foreveross.bsl.mam.device.application.vo.DeviceUserVO;
import com.foreveross.bsl.mam.model.device.domain.DeviceUser;

/**
 * 设备管理实现
 * @author tanzhixiong
 * @2013-6-21 @下午4:41:43
 */
@Named("deviceUserApplication")
public class DeviceUserApplicationImpl implements DeviceUserApplication{

	@Override
	public DeviceUserVO saveEntity(DeviceUserVO entity) {
		DeviceUser deviceUser = new DeviceUser();
		BeanHelper.copyProperties(deviceUser, entity);
		deviceUser.save();
		entity.setId(deviceUser.getId());
		return entity;
	}

	@Override
	public DeviceUserVO updateEntity( String id, DeviceUserVO entity) {
		DeviceUser device = new DeviceUser();
		device = (DeviceUser)device.findOne(id);
		BeanHelper.copyProperties(device, entity);
		device.save();
		BeanHelper.copyProperties(entity, device);
		return entity;
	}

	@Override
	public void deleteEntity( String id) {
		DeviceUser device = new DeviceUser();
		device = (DeviceUser) device.findOne(id);
		device.delete();
	}

	@Override
	public DeviceUserVO getEntity(String id) {
		DeviceUser device = new DeviceUser();
		device = (DeviceUser) device.findOne(id);
		DeviceUserVO devicevo = new DeviceUserVO();
		BeanHelper.copyProperties(devicevo, device);
		return devicevo;
	}

	@Override
	public List<DeviceUserVO> findAll() {
		DeviceUser device = new DeviceUser();
		Iterable iterable = device.findAll();
		if(iterable != null){
			List<DeviceUserVO> result = new ArrayList<DeviceUserVO>();
			DeviceUserVO vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				DeviceUser obj = (DeviceUser) iterator.next();
				vo = new DeviceUserVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	@Override
	public Page<DeviceUserVO> findByPageable(Pageable pageable) {
		DeviceUser device = new DeviceUser();
		Page page = device.findAll(pageable);
		if(page != null && page.getContent() != null){
			List<DeviceUserVO> result = new ArrayList<DeviceUserVO>();
			DeviceUserVO vo;
			for (Iterator iterator = page.getContent().iterator(); iterator.hasNext();) {
				DeviceUser obj = (DeviceUser) iterator.next();
				vo = new DeviceUserVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<DeviceUserVO> pageVO = new PageImpl<DeviceUserVO>(result, pageable, page.getTotalElements());
			return pageVO;
		}
		return null;
	}



}
